﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Shell;
using PoznanEvents.Models;
using System;
using System.Windows;
using System.Windows.Controls;

namespace PoznanEvents
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            //EventsMap.CredentialsProvider = new ApplicationIdCredentialsProvider("AsNqgjle8C4_dyrcgfaqiIO1Cggmf6bLLvThv0sIDx6HJ8X59gEJiT0W8XR5Mbg1");
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                if (!App.ViewModel.IsDataLoaded)
                {
                    CloseSearch(this, new EventArgs());
                    SetLoadingBar(this, new EventArgs());
                    CloseApplicationBar(this, new EventArgs());
                    App.ViewModel.LoadData();
                }
            }
            else
            {
                ShowInterNetConnectionFaildAlert(this, new EventArgs());
            }

            App.ViewModel.CloseLoadingBar += CloseLoadingBar;
            App.ViewModel.ShowInterNetConnectionFaildAlert += ShowInterNetConnectionFaildAlert;
            App.ViewModel.ShowApplicationBar += ShowApplicationBar;
            App.ViewModel.ShowSearch += ShowSearch;
            DataContext = App.ViewModel;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.IsDataChanged)
            {
                CloseSearch(this, new EventArgs());
                CloseApplicationBar(this, new EventArgs());
                App.ViewModel.ClearAllData();
                SetLoadingBar(this, new EventArgs());
                App.ViewModel.LoadData();
            }
        }

        private void NavigateToEventView(Event item)
        {
            if (item != null)
            {
                App.ViewModel.SelectedEvent = item;
                var page = "/EventPage.xaml";
                NavigationService.Navigate(new Uri(page, UriKind.Relative));
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingPage.xaml", UriKind.Relative));
        }

        private void RefreshBarIconButton_Click(object sender, EventArgs e)
        {
            CloseApplicationBar(this, new EventArgs());
            App.ViewModel.ClearAllData();
            SetLoadingBar(this, new EventArgs());
            App.ViewModel.LoadData();
        }

        public void SetLoadingBar(object sender, EventArgs args)
        {
            LoadingProgressBar.Visibility = Visibility.Visible;
        }

        public void CloseLoadingBar(object sender, EventArgs args)
        {
            LoadingProgressBar.Visibility = Visibility.Collapsed;
        }

        public void ShowInterNetConnectionFaildAlert(object sender, EventArgs args)
        {
            MessageBox.Show("Brak połączenia z internetem");
            LoadingProgressBar.Visibility = Visibility.Collapsed;
            CloseApplicationBar(this, new EventArgs());
            SearchButton.Visibility = Visibility.Collapsed;
            SearchTextBox.Visibility = Visibility.Collapsed;
        }

        public void CloseApplicationBar(object sender, EventArgs args)
        {
            ApplicationBar.IsVisible = false;
        }

        public void ShowApplicationBar(object sender, EventArgs args)
        {
            ApplicationBar.IsVisible = true;
        }

        public void ShowSearch(object sender, EventArgs args)
        {
            SearchButton.Visibility = Visibility.Visible;
            SearchTextBox.Visibility = Visibility.Visible;
        }

        public void CloseSearch(object sender, EventArgs args)
        {
            SearchButton.Visibility = Visibility.Collapsed;
            SearchTextBox.Visibility = Visibility.Collapsed;
        }

        //public void LoadPansToMap (object sender, MapPansEvenArgs args)
        //{
        //    foreach (var eventItem in args.GetMapPans())
        //    {
        //        Pushpin pin = new Pushpin();
        //        pin.Background = new SolidColorBrush(Colors.Yellow);
        //        pin.Foreground = new SolidColorBrush(Colors.Black);
        //        pin.Location = new GeoCoordinate(eventItem.EventLatitude, eventItem.EventLongitue);
        //        if (eventItem.EventTitle.Length > 20)
        //            pin.Content = eventItem.EventTitle.Substring(0, 20);
        //        else
        //            pin.Content = eventItem.EventTitle;

        //        pin.Tap += (obj, a) =>
        //        {
        //                NavigateToEventView(eventItem);
        //        };
        //        EventsMap.Children.Add(pin);
        //    }

        //    EventsMap.SetView(new GeoCoordinate(52.4166667, 16.9666667, 200), 11);
        //}

        private void Events_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (Event)Events.SelectedItem;
                NavigateToEventView(item);
        }

        private void FindBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/DateFilterPage.xaml", UriKind.Relative));
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            CloseSearch(this, new EventArgs());
            CloseApplicationBar(this, new EventArgs());
            SetLoadingBar(this, new EventArgs());
            App.ViewModel.SearchText = SearchTextBox.Text;
            App.ViewModel.LoadLocalData();
        }
    }
    
}